package com.xjcs.saplingplan.dialog

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import com.lxj.xpopup.core.CenterPopupView
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.toColor
import com.xjcs.saplingplan.databinding.PopRemindTimeBinding
import java.time.LocalDateTime
import java.util.Calendar
import java.util.TimeZone

class RemindTimePop(
    context: Context,
    private var localDate: LocalDateTime,
    private val callback: (action: Long) -> Unit
) : CenterPopupView(context) {

    private var pvTime: TimePickerView? = null

    private lateinit var binding: PopRemindTimeBinding

    override fun getImplLayoutId(): Int {
        return R.layout.pop_remind_time
    }

    override fun onCreate() {
        super.onCreate()
        binding = PopRemindTimeBinding.bind(contentView)
        initWheelView()
        initListener()
    }

    private fun initWheelView(){
        val viewGroup = binding.frameLayout as ViewGroup
        val curCalendar = Calendar.getInstance()
        curCalendar.timeZone = TimeZone.getDefault()
        curCalendar.set(Calendar.HOUR_OF_DAY, localDate.hour)
        curCalendar.set(Calendar.MINUTE, localDate.minute)
        val startCalendar = Calendar.getInstance()
        startCalendar.set(Calendar.HOUR_OF_DAY, 0)
        startCalendar.set(Calendar.MINUTE, 0)
        val endCalendar = Calendar.getInstance()
        endCalendar.set(Calendar.HOUR_OF_DAY, 23)
        endCalendar.set(Calendar.MINUTE, 59)
        pvTime = TimePickerBuilder(context) { date, _ ->
            callback.invoke(date.time)
        }.setDate(curCalendar)
            .setRangDate(startCalendar, endCalendar)
            .setLayoutRes(R.layout.wheelview_select_date) {
                it.findViewById<View>(R.id.space1).visibility = View.VISIBLE
                it.findViewById<View>(R.id.space2).visibility = View.VISIBLE
            }
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .setLabel("", "", "", "", "", "")
            .setContentTextSize(23)
            .setSubCalSize(18)
            .setItemVisibleCount(3)
            .setLineSpacingMultiplier(2.5f)
            .isCyclic(false)
            .isAlphaGradient(true)
            .setTextColorCenter(Color.BLACK)
            .setTextColorOut("#B3000000".toColor())
            .setDecorView(viewGroup)
            .setOutSideColor(0xFFFFFF)
            .setOutSideCancelable(false)
            .setDividerColor(Color.TRANSPARENT)
            .build()

        pvTime?.show(viewGroup, false)
    }

    private fun initListener() {
        //取消
        binding.tvCancel.setSingleClick {
            dismiss()
        }
        //确定
        binding.tvSure.setSingleClick {
            pvTime?.returnData()
            dismiss()
        }
    }

}